import asyncio
import time
import requests
from config import LiveConfig
from concurrent.futures import ThreadPoolExecutor

@asyncio.coroutine
def get_data(_loop, url):
    # executor = ThreadPoolExecutor(1)
    tmp = yield from _loop.run_in_executor(None, start_request, url)
    return tmp


def start_request(url):
    index = url.split("page", 1)
    print('start', index[1])
    r = requests.get(url)
    try:
        data = r.json()
        data = data['data']
    except ValueError:
        data = None
    return data


loop = asyncio.get_event_loop()
tasks = [asyncio.ensure_future(get_data(loop, _)) for _ in LiveConfig.URLS]
start = time.time()
loop.run_until_complete(asyncio.wait(tasks))
end = time.time()
diff = (end - start) * 1000
print('diff', diff)
# print([_.result() for _ in tasks][0])